﻿using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using Microsoft.VisualBasic.FileIO;

namespace FileNameEdit8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //默认操作是替换
        private static MyOperateEnum _radioOperate = MyOperateEnum.Replace;
        public MainWindow()
        {
            InitializeComponent();
        }

        //执行操作
        private void Button_OnClick(object sender, RoutedEventArgs e)
        {
            //绝对物理地址
            var filesUrl = TextBoxDirectory.Text;
            if (string.IsNullOrEmpty(filesUrl))
            {
                MessageBox.Show("文件地址不能为空");
                return;
            }
            var valueText = TextBoxValue.Text;
            if (string.IsNullOrEmpty(valueText) && _radioOperate != MyOperateEnum.Directory)
            {
                MessageBox.Show("被替换、删除和排序文字不能为空");
                return;
            }
            var replaceText = TextBoxReplace.Text;
            if (_radioOperate == MyOperateEnum.Replace && string.IsNullOrEmpty(replaceText))
            {
                MessageBox.Show("替换操作时需要替换的文字不能为空");
                return;
            }
            if (_radioOperate == MyOperateEnum.Order && !RegexNumber(valueText))
            {
                MessageBox.Show("排序操作时，输入的必须是数字");
                return;
            }
            //获取所有的文件名，不管什么格式
            DirectoryInfo folder = new DirectoryInfo(filesUrl);
            FileInfo[] files = folder.GetFiles();
            foreach (var fileInfo in files)
            {
                var newFileName = fileInfo.Name;
                //当存在需要替换或者删除的字符时再操作
                if (fileInfo.Name.Contains(valueText) && _radioOperate != MyOperateEnum.Order && _radioOperate != MyOperateEnum.Directory)
                {
                    //替换操作
                    if (_radioOperate == MyOperateEnum.Replace)
                    {
                        newFileName = newFileName.Replace(valueText, replaceText);
                    }
                    //删除操作
                    else if (_radioOperate == MyOperateEnum.Delete)
                    {
                        var index = newFileName.IndexOf(valueText, StringComparison.Ordinal);
                        newFileName = newFileName.Remove(index, valueText.Length);
                    }
                }
                //排序操作
                else if (_radioOperate == MyOperateEnum.Order)
                {
                    //S01E01(下标)
                    var startIndex = newFileName.IndexOf("S0", StringComparison.Ordinal);
                    //E01
                    var sStr = newFileName.Substring(startIndex + 3, 3);
                    //01
                    var numberStr = newFileName.Substring(startIndex + 4, 2);
                    //2
                    var newNumber = Convert.ToInt32(numberStr) + Convert.ToInt32(valueText);
                    //02
                    var newNumberStr = newNumber.ToString().PadLeft(2, '0');
                    //E02
                    var newSStr = sStr.Replace(numberStr, newNumberStr);
                    //S01E02
                    newFileName = newFileName.Replace(sStr, newSStr);
                }
                //目录操作
                else if (_radioOperate == MyOperateEnum.Directory)
                {
                    //去除后缀名
                    var folderName = fileInfo.Name.Replace(fileInfo.Extension, "");
                    //创建新的子目录
                    var newfolder = folder.CreateSubdirectory(folderName);
                    //移动当前文件到子目录
                    fileInfo.MoveTo(Path.Combine(newfolder.FullName, newFileName));
                    continue;
                }
                else
                {
                    MessageBox.Show("意外错误");
                }
                FileSystem.RenameFile(fileInfo.FullName, newFileName);
            }

            MessageBox.Show("修改成功");
        }

        //单选按钮
        private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
        {
            var rb = sender as RadioButton;
            if (rb?.IsChecked == true)
            {
                _radioOperate = rb.Name switch
                {
                    "R1" => MyOperateEnum.Replace,
                    "R2" => MyOperateEnum.Delete,
                    "R3" => MyOperateEnum.Order,
                    "R4" => MyOperateEnum.Directory,
                    _ => _radioOperate
                };
            }
        }

        //操作类型
        private enum MyOperateEnum
        {
            Replace,//替换
            Delete,//删除
            Order,//排序
            Directory//目录
        }

        //判断是否是数字
        private bool RegexNumber(string? input)
        {
            var reg = new Regex("^[1-9]\\d*$");
            return input != null && reg.IsMatch(input);//返回值可以是bool,可以用isMatch.
        }

    }
}